package com.zhuiyun.project.api.videosorting.controller;

import com.zhuiyun.project.api.imagesorting.entity.ImageSorting;
import com.zhuiyun.project.api.videosorting.entity.VideoSorting;
import com.zhuiyun.project.api.videosorting.service.VideoSortingService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.List;

/**
 * @ClassName VideoSorting
 * @Description  视频排序controller层
 * @Date 2024/12/9 19:18
 */
@RestController
@RequestMapping("/videoSorting")
@CrossOrigin
public class VideoSortingController {
    @Autowired
    private VideoSortingService videoSortingService;

   /**
   * @Author zsc
   * @Description 新增视频排序
   * @Date 2024/12/9 19:29
   * @Return com.zhuiyun.project.common.response.CommonResult
   * @param multipartRequest
   * @param videoSorting
   */
    @PostMapping("/saveVideoSorting")
    public CommonResult saveVideoSorting(MultipartRequest multipartRequest, VideoSorting videoSorting) throws CommonException {
        MultipartFile img = multipartRequest.getFile("img");
        MultipartFile video = multipartRequest.getFile("video");
        if (videoSorting != null){
            int i = videoSortingService.saveVideoSorting(videoSorting,img,video);
            return CommonResult.ok(i);
        }
        return CommonResult.ok();
    }

    /**
    * @Author zsc
    * @Description 删除视频排序
    * @Date 2024/12/9 19:32
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param id
    * @param imgId
    */
    @DeleteMapping("/deleteVideoSorting")
    public CommonResult deleteVideoSorting(@RequestParam("id") int id, @RequestParam("imgId")int imgId,
                                           @RequestParam("videoId")int videoId) throws CommonException {
        if (id > 0){
            int i = videoSortingService.deleteVideoSorting(id,imgId,videoId);
            return CommonResult.ok(i);
        }
        return CommonResult.ok();
    }

    /**
    * @Author zsc
    * @Description 修改视频排序
    * @Date 2024/12/9 19:32
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param multipartRequest
    * @param videoSorting
    */
    @PutMapping("/updateVideoSorting")
    public CommonResult updateVideoSorting(MultipartRequest multipartRequest, VideoSorting videoSorting) throws CommonException {
        MultipartFile img = multipartRequest.getFile("img");
        MultipartFile video = multipartRequest.getFile("video");
        if (videoSorting != null){
            int i = videoSortingService.updateVideoSorting(videoSorting,img,video);
            return CommonResult.ok(i);
        }
        return CommonResult.ok();
    }

   /**
   * @Author zsc
   * @Description 查询视频排序
   * @Date 2024/12/9 19:31
   * @Return com.zhuiyun.project.common.response.CommonResult
   */
    @GetMapping("/getVideoSortingList")
    public CommonResult getVideoSortingList(){
        List<VideoSorting> lists= videoSortingService.getVideoSortingList();
        return CommonResult.ok(lists);
    }
}
